package action;

import Impl.adminDaoImpl;
import Impl.userDaoImpl;
import entity.Admin;
import entity.User;
import service.LoginService;
import service.LoginServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.List;

@WebServlet(name = "LoginAction")
public class LoginAction extends HttpServlet {
    private static final long serialVersionUID = 2149515480031549266L;

    private LoginService service = new LoginServiceImpl();

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        System.out.println("-------正在执行LoginAction---------");
        String name = request.getParameter("name");
        String pwd = request.getParameter("password");
        String method = request.getParameter("method");
        HttpSession session =request.getSession();
        if (method!=null &&method.equals("admin")){
            System.out.println("管理员登录：");
            if(!service.isNormalCode(request)){
                request.setAttribute("message", "验证码不正确！");
                System.out.println("验证码不正确");
                request.getRequestDispatcher("/login.jsp").forward(request, response);
                return;
            }
            adminDaoImpl adi = new adminDaoImpl();
            Admin admin = adi.findAdminByName(name);
            if (admin==null||!admin.getApwd().equals(pwd)){
                request.setAttribute("message", "用户名或密码错误！");
                System.out.println("用户名或密码错误");
                request.getRequestDispatcher("/login.jsp").forward(request, response);
                return;
            }
            session.setAttribute("admin",admin);
            if (admin.getIdentity().equals("超级管理员")){
                request.getRequestDispatcher("/index.jsp").forward(request,response);
            }else {
                request.getRequestDispatcher("/other_index.jsp").forward(request,response);
            }


        }else{
            System.out.println("用户登录：");
            if(!service.isNormalCode(request)){
                request.setAttribute("message", "验证码不正确！");
                System.out.println("验证码不正确");
                request.getRequestDispatcher("/user/user_login.jsp").forward(request, response);
                return;
            }
            userDaoImpl userDao = new userDaoImpl();
            User user = userDao.findUserByName(name);
            System.out.println(user.getUid());
            System.out.println(user.getUpassword());
            if (user==null||!user.getUpassword().equals(pwd)){
                request.setAttribute("message", "用户名或密码错误！");
                System.out.println("用户名或密码错误");
                request.getRequestDispatcher("/user/user_login.jsp").forward(request, response);
                return;
            }
            session.setAttribute("user",user);
            request.getRequestDispatcher("showblock.jhtml").forward(request,response);

        }
        System.out.println("登录成功！");
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request,response);
    }
}
